/**
 * 系统菜单
 * @author Canaan
 * @date 2019/8/12 17:55
 */
export default {
  state: {
    navMenu: {
      loading: false,     //数据是否加载中
      options: []         //具体的选项列表
    }
  },

  mutations: {
    clearNavMenu({navMenu}) {
      navMenu = {
        loading: false,
        options: []
      }
    },
    navMenu({navMenu}, {cmd, val}) {
      switch (cmd) {
        case "loading":
          navMenu.loading = val;
          break;
        case "options":
          navMenu.options = val;
          break;
        default:
          throw "navMenu 未知的命令" + cmd;
      }
    }
  },

  actions: {
    loadNavMenu({commit, state}, vue) {
      //sessionStorage 没有token 那么不加载
      const token = sessionStorage.getItem(vue.$env.LOGIN_TOKEN);
      if (!token) {
        return;
      }
      //如果正在加，就不处理
      if (state.navMenu.loading) {
        return;
      }

      commit('navMenu', {cmd: "loading", val: true});
      return vue.$ajax.get('auth/current_view/nav')
        .then(res => {
          commit('navMenu', {cmd: "loading", val: false});
          if (res.code !== 'ok') {
            throw res.msg;
          }
          commit('navMenu', {cmd: "options", val: res.data || []});
          return res.data;
        });
    }
  },
  getters: {
    /**
     * 侧边导航栏
     * @author Canaan
     * @date 2019/8/13 10:14
     */
    drawerMenu({navMenu}) {
      let arr = navMenu.options || [];
      return arr.filter(item => {
        return item.type === 'NAV_BAR';
      });
    },
    /**
     * 主页菜单
     * @author Canaan
     * @date 2019/8/13 10:17
     */
    indexMenu({navMenu}) {
      //要选择的菜单
      return navMenu.options.filter(item => {
        if (item.type !== 'NAV_BAR') {
          return false;
        }
        // return ['物料信息','入库记录','预警中心','出库记录','统计报表'].includes(item.name);
        return ['materialIndex',
          'inventoryIndex',
          'forewarningIndex',
          'inventoryTraceIndex',
          'storeReportIndex',
          'brandReportIndex'].includes(item.menuCode);
      });
    }


  }
}
